package com.BlogPark.controller;

import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.mapper.CollectionsMapper;
import com.BlogPark.mapper.CommentsMapper;
import com.BlogPark.mapper.LikesMapper;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.DetailsUser;
import com.BlogPark.service.SearchService.SearchService;
import com.BlogPark.service.UserService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 业峰
 * @Date: 2022/8/11 - 08 - 11 - 14:51
 * @version: 1.0
 * 搜索响应：包括1.查看具体文章、2.输入词条搜索相关文章、3.条件筛选(待定)
 */
@RestController
@RequestMapping("/searchArticle")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikesMapper likesMapper;
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CollectionsMapper collectionsMapper;

    /**
     * 1.查看具体文章
     *
     * @param id 根据id查询具体的文章
     * @return
     */
    @GetMapping("/searchById/{id}")
    public Result searchById(@PathVariable("id") String id) throws Exception {
        Integer code;
        String msg = "";
        Article article = searchService.searchById(id);
        //判断是否查询到数据
        if (article == null) {
            msg = "查询不到该文章";
            code = Code.GET_ERR;
        } else {
            msg = "查询成功";
            code = Code.GET_OK;
        }
        System.out.println("根据id查询具体的文章成功");
        return new Result(code, article, msg);
    }

    /**
     * 2.输入词条搜索相关文章
     *
     * @param entry 从表单中输入的词条
     * @return 返回所有的文章列表
     */
    @GetMapping("/searchByEntry")
    public Result searchByEntry(@RequestParam("entry") String entry) {
        //查询标题、标签是对应字段的文章列表
        Integer code;
        String msg;
        entry = "%" + entry + "%";
        List<Article> articles = searchService.SearchByEntry(entry);

        //System.out.println(articles);
        //System.out.println("kong" + likesMapper);
        //判断是否查到文章列表
        if (!articles.isEmpty()) {
            for (Article article : articles) {
                DetailsUser detailsUser = userService.searchById(article.getAutherId());
                if (detailsUser != null) {
                    //设置文章作者名称
                    article.setAutherName(detailsUser.getName());
                    article.setLikes(likesMapper.getLikes(article.getId()));
                    article.setComments(commentsMapper.getComments(article.getId()));
                    article.setCollection(collectionsMapper.getCollections(article.getId()));
                } else {
                    article.setAutherName("该用户不存在");
                }
            }
            code = Code.GET_OK;
            msg = "查询成功";
            return new Result(code, articles, msg);
        } else {
            msg = "没有查到相应的文章";
            code = Code.GET_ERR;
            //返回所有文章
            List<Article> allArticles = searchService.showAll();
            for (Article article : allArticles) {
                DetailsUser detailsUser = userService.searchById(article.getAutherId());
                if (detailsUser != null) {
                    article.setAutherName(detailsUser.getName());
                    article.setLikes(likesMapper.getLikes(article.getId()));
                    article.setComments(commentsMapper.getComments(article.getId()));
                    article.setCollection(collectionsMapper.getCollections(article.getId()));
                } else {
                    article.setAutherName("该用户不存在");
                }
            }
            return new Result(code, allArticles, msg);
        }

    }

    /**
     * 综合搜索获取全部文章
     * @return
     */
    @GetMapping("/showAllArticles")
    public Result showAllArticles() {
        Integer code;
        String msg;
        List<Article> allArticles = searchService.showAll();
        if (allArticles != null && !allArticles.isEmpty()) {
            code = Code.GET_OK;
            msg = "全部文章获取成功";
            for (Article article : allArticles) {
                DetailsUser detailsUser = userService.searchById(article.getAutherId());
                if (detailsUser != null) {
                    article.setAutherName(detailsUser.getName());
                    article.setLikes(likesMapper.getLikes(article.getId()));
                    article.setComments(commentsMapper.getComments(article.getId()));
                    article.setCollection(collectionsMapper.getCollections(article.getId()));
                } else {
                    article.setAutherName("该用户不存在");
                }
            }
            return new Result(code, allArticles, msg);
        } else {
            code = Code.GET_ERR;
            msg = "全部文章获取失败";
            return new Result(code, null, msg);
        }

    }

}
